package com.taomeishu.mailcrazy.checkers;

import com.taomeishu.mailcrazy.AbstractMailExistChecker;
import com.taomeishu.mailcrazy.CheckResult;
import com.taomeishu.mailcrazy.Mail;
import com.taomeishu.mailcrazy.http.ClientResponse;
import com.taomeishu.mailcrazy.parser.ResultParseException;
import com.taomeishu.mailcrazy.parser.ResultParser;

public class Net263MailExistChecker extends AbstractMailExistChecker {

	private String baseUrl = "http://mbill.263.net/frontuser/checkname.do?method=checkEmailAcc&accountName=";
	
	private Net263ResultParser net263ResultParser = new Net263ResultParser();
	
	@Override
	protected String makeRequestUrl(String username, String domainName) {
		return baseUrl + username + "@" + domainName;
	}

	@Override
	protected ResultParser getResultParser() {
		return net263ResultParser;
	}
	
	static class Net263ResultParser implements ResultParser {

		@Override
		public CheckResult parse(ClientResponse clientResponse, String username, String domainName) throws ResultParseException {
			String s = (new String(clientResponse.getData())).trim();
			CheckResult result = new CheckResult(true);
			Mail mail = new Mail(username, domainName);
			result.setMail(mail);
			if (s.equals("0")) {
				result.addNotExistMail(mail);
			} else {
				// exist
				result.addExistMail(mail);
			}
			return result;
		}
		
	}

}
